package com.logos.musicframe.service.impl;

import com.logos.musicframe.constant.RedisConstant;
import com.logos.musicframe.converter.SongPlaylistConverter;
import com.logos.musicframe.entity.SongsPlaylist;
import com.logos.musicframe.mapper.SongsPlaylistMapper;
import com.logos.musicframe.req.SongsPlaylistReq;
import com.logos.musicframe.service.SongsPlaylistService;
import com.logos.musicframe.utils.RedisUtil;
import com.logos.musicframe.utils.UserInfoUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


/**
 * 歌曲与歌单映射表(SongsPlaylist)表服务实现类
 *
 * @author rose
 * @since 2025-06-14 21:31:49
 */
@Service("songsPlaylistService")
public class SongsPlaylistServiceImpl implements SongsPlaylistService {
    @Autowired
    private SongsPlaylistMapper songsPlaylistMapper;

    @Autowired
    private RedisUtil redisUtil;

    @Autowired
    private UserInfoUtil userInfoUtil;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public SongsPlaylist queryById(Long id) {
        return this.songsPlaylistMapper.queryById(id);
    }

    /**
     * 新增数据
     *
     * @param songsPlaylist 实例对象
     * @return 实例对象
     */
    @Override
    public SongsPlaylist insert(SongsPlaylist songsPlaylist) {
        this.songsPlaylistMapper.insert(songsPlaylist);
        return songsPlaylist;
    }

    /**
     * 修改数据
     *
     * @param songsPlaylist 实例对象
     * @return 实例对象
     */
    @Override
    public SongsPlaylist update(SongsPlaylist songsPlaylist) {
        this.songsPlaylistMapper.update(songsPlaylist);
        return this.queryById(songsPlaylist.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Long id) {
        return this.songsPlaylistMapper.deleteById(id) > 0;
    }

    /**
     * 添加歌曲到歌单
     *
     * @param songsPlaylistReq
     * @return
     */
    @Override
    public Boolean addSong(SongsPlaylistReq songsPlaylistReq) {
        // 先检查歌曲是否已经存在于歌单中
        if (checkSongExists(songsPlaylistReq.getSongId(), songsPlaylistReq.getPlaylistId())) {
            return false; // 已存在，不需要重复添加
        }
        
        SongsPlaylist songsPlaylist = SongPlaylistConverter.INSTANCE.convertReqToEntity(songsPlaylistReq);
        int count = songsPlaylistMapper.insert(songsPlaylist);
        if (count <= 0) {
            return false;
        }
        // 删除单个歌单缓存和所有歌单缓存
        Integer playlistId = songsPlaylist.getPlaylistId();
        redisUtil.del(RedisConstant.PLAYLIST_ID + playlistId);
        redisUtil.del(RedisConstant.PLAYLIST_CREATED_USER + userInfoUtil.getUserId());
        return count > 0;
    }
    
    /**
     * 检查歌曲是否已经存在于歌单中
     *
     * @param songId 歌曲ID
     * @param playlistId 歌单ID
     * @return 是否存在
     */
    @Override
    public Boolean checkSongExists(Integer songId, Integer playlistId) {
        return songsPlaylistMapper.checkSongExists(songId, playlistId) > 0;
    }
}
